<template>
  <el-dialog
    v-model="model"
    :close-on-click-modal="false"
    :title="title"
    width="415px"
    @close="model = false"
  >
    <div class="form">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        class="demo-ruleForm"
        status-icon
      >
        <el-form-item prop="id">
          <div>请输入班级ID（多个班级，用逗号分隔）</div>
          <el-input v-model="ruleForm.id" placeholder="请输入班级ID" />
        </el-form-item>
        <el-form-item prop="url">
          <div class="link-wrapper">
            <div>可将下方链接发给学员</div>
            <div class="link">
              <el-input ref="linkRef" v-model="ruleForm.url" />
              <el-button :disabled="!ruleForm.url" :style="{marginLeft: '10px'}" type="primary" link @click="copyLink">复制链接</el-button>
            </div>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="model = false">取消</el-button>
        <el-button type="primary" @click="model = false">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const title = ref('班级开放签订')
    const model = ref(false)
    const linkRef = ref()
    const ruleFormRef = ref()
    const ruleForm = reactive({
      id: '',
      url: ''
    })
    const rules = reactive({
      id: [
        { required: true, message: '请输入班级ID', trigger: 'blur' }
      ],
      url: [
        { required: true, message: '请输入链接', trigger: 'blur' }
      ]
    })
    const options = [
      {
        value: 'Option1',
        label: 'Option1'
      },
      {
        value: 'Option2',
        label: 'Option2'
      },
      {
        value: 'Option3',
        label: 'Option3'
      },
      {
        value: 'Option4',
        label: 'Option4'
      },
      {
        value: 'Option5',
        label: 'Option5'
      }
    ]
    // 复制链接
    const copyLink = () => {
      navigator.clipboard.writeText(ruleForm.url).then(() => {
        ElMessage.success('复制成功')
      })
    }
    const show = () => {
      model.value = true
    }
    return {
      title,
      model,
      linkRef,
      ruleFormRef,
      ruleForm,
      rules,
      options,
      show,
      copyLink
    }
  }
}

</script>

<style scoped lang="scss">
.code {
  display: flex;
}
.link-wrapper {
  width: 100%;
  .link {
    display: flex;
    flex-direction: row;
  }
}
:deep(.el-input) {
  &::before {
    display: block;
    position: absolute;
    left: -10px;
    content: "*";
    color: red;
  }
}
</style>